package com.heima.wemedia.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;


    /**
     * 素材上传
     *
     * @param file
     * @return
     */
    @Override
    @Transactional
    public ResponseResult upload(MultipartFile file) {
        //1.上传图片到minio中
        try {
            //1.1 获取源文件名称
            String originalFilename = file.getOriginalFilename();
            //1.2获取后缀名
            String subfixx = originalFilename.substring(originalFilename.lastIndexOf("."));
            //1.3 定义文件名称
            String filename= UUID.randomUUID().toString().replace("-","")+subfixx;

            String path = fileStorageService.uploadImgFile("", filename, file.getInputStream());
        //2.保存数据到wm_material表中
            WmMaterial entity=new WmMaterial();
            entity.setUserId(WmThreadLocalUtil.getUser().getId());
            entity.setUrl(path);
            entity.setType((short)0);
            entity.setIsCollection((short)0);
            entity.setCreatedTime(new Date());

            super.save(entity);
        //3.返回数据
            return ResponseResult.okResult(entity);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //1.非空判断
        dto.checkParam();
        //2.分页查询wm_material表
        IPage<WmMaterial> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> queryWrapper= Wrappers.lambdaQuery();
        //是否收藏条件
        if(dto.getIsCollection()!=0){
            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        //用户id
        queryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        page = super.page(page, queryWrapper);

        //3.返回数据
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }
}
